#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年3月8日

@author: Administrator
'''
from common.decorators import login_required
from django.db.models.query_utils import Q
from django.http.response import HttpResponse
from django.template.response import TemplateResponse
from funcresource import settings
from funcresource.decorators import user_funcresource
from funcresource.models import  UserResource
import copy
import ujson as json


@login_required
@user_funcresource
def index(request, template="funcresource/admin/userresource/userresource.html"):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    return TemplateResponse(request, template, rsp_data)
    
@login_required
def list(request):
    """
        func:
                               用户资源列表
        parameters:
            start:开始下标
            length:每页展示个数
            search[value]:搜索关键字
            columns[4][search][value]:状态
            draw:渲染次数
    """
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    rsp_data["data"] = []
    start = int(request.POST['start'])
    length = int(request.POST['length'])
    keyword = request.POST["search[value]"]
    draw = request.POST['draw']
    state = request.POST["columns[4][search][value]"]
    state = '' if state == '' else state[1:-1]
    q = Q()
    if keyword:
        q.add(Q(**{"user__username__icontains": keyword}), Q.OR)
        q.add(Q(**{"resource__name__icontains": keyword}), Q.OR)
    if state:
        q.add(Q(**{"state": state}), Q.AND)
    
    count = UserResource.objects.filter(q).exclude(state=settings.StateEnum.TEMPORARY).count()
    userresources = UserResource.objects.filter(q).exclude(state=settings.StateEnum.TEMPORARY)[start:start + length]
    for userresource in userresources:
        record = {
            "id":userresource.id,
            "username":userresource.user.username,
            'state':userresource.state,
            'state_desc':settings.get_state_desc(userresource.state),
            "resource_name":userresource.resource.name,
            "resource_parent_name":userresource.resource.parent.name if userresource.resource.parent else "",
        }
        rsp_data["data"].append(record)
    rsp_data["recordsTotal"] = count
    rsp_data["recordsFiltered"] = count
    rsp_data["draw"] = draw
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')

@login_required
def delete(request):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    userresource_ids = request.POST["userresource_ids"]
    if userresource_ids:
        userresource_ids = userresource_ids.split(",")
    UserResource.objects.filter(id__in=userresource_ids, state=settings.StateEnum.VALID).update(state=settings.StateEnum.DELETED)
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')

@login_required
def reopen(request):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    userresource_ids = request.POST["userresource_ids"]
    if userresource_ids:
        userresource_ids = userresource_ids.split(",")
    UserResource.objects.filter(id__in=userresource_ids, state=settings.StateEnum.DELETED).update(state=settings.StateEnum.VALID)
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')
